package com.example.weibo3.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.weibo3.R;
import com.example.weibo3.bean.MessageItem;
import com.example.weibo3.bean.WeiboItem;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class WeiboAdapter extends ArrayAdapter<WeiboItem> {
    public WeiboAdapter(@NonNull Context context, int resource, @NonNull List<WeiboItem> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    // 返回每一条微博
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        // 加载布局文件，生成一个view
        View view = LayoutInflater.from(getContext()).inflate(R.layout.weibo_list_item, parent, false);

        // 获取数据，通过position获取数组中对应的一项数据
        WeiboItem messageItem = getItem(position);

        // 什么数据渲染到什么组件上
        // 头像图片渲染填充
        ImageView imageView = view.findViewById(R.id.imageView);
        Glide.with(getContext())
                .load(messageItem.getAvatar())
                .into(imageView);

        // 用户名
        TextView textView = view.findViewById(R.id.webo_card_name);
        textView.setText(messageItem.getName());
        // 微博内容
        TextView contentView = view.findViewById(R.id.content);
        contentView.setText(messageItem.getContent());

        TextView shareView = view.findViewById(R.id.share);
        shareView.setText(messageItem.getShares()+"");

        TextView commentView = view.findViewById(R.id.comment);
        commentView.setText(messageItem.getComments()+"");

        final TextView likeView = view.findViewById(R.id.like);
        likeView.setText(messageItem.getLikes()+"");
        likeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id = messageItem.get_id();    // 获取微博的id
                // 1. 定义map 键值对对象
                Map<String, String> map = new HashMap<>();
                map.put("id", id);            // 传一个id
                // 2. 把对象转成字符串，通过gson
                Gson gson = new Gson();
                String jsonStr = gson.toJson(map);
                // 3. 发起请求，使用okhttp
                // ===============网络请求========================
                OkHttpClient client = new OkHttpClient();    // 初始化client对象

                // 定义requestbody对象，把传递的参数放入到body当中
                RequestBody requestBody = RequestBody.create(jsonStr, MediaType.parse("application/json; charset=utf-8"));

                // 配置请求参数, post请求
                Request request = new Request.Builder()
                        .url("http://129.28.159.96:7001/v1/test/like")     // 请求的接口地址
                        .post(requestBody)                             // 放入请求参数
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        // 错误处理逻辑
                        Log.e("tag", e.toString());
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        // 成功处理逻辑
                        String data = response.body().string();
                        Log.i("tag", data);
                        // 更新界面计数，先获取原来的计数，然后+1，再进行显示
                        ((Activity)getContext()).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // 先获取listview原来的值，然后计数+1，再+“”转成字符串
                                likeView.setText(Integer.parseInt(likeView.getText().toString()) + 1 + "");
                            }
                        });

                    }
                });
                // ================================================

            }
        });

        TextView timeView = view.findViewById(R.id.time);
        timeView.setText(messageItem.getDate());

        // 取图片数据，每一条微博的所有图片数据
        List<String> images = messageItem.getImages();
        GridViewAdapter gridViewAdapter =
                new GridViewAdapter(getContext(), R.layout.gridview_item, images);

        GridView gridView = view.findViewById(R.id.image_grid_view);
        gridView.setAdapter(gridViewAdapter);
        return view;
    }
}
